from django import http
from django import forms
from django.conf import settings
from django.core import serializers
from django.template import loader

from EE.models import Game
from EE.models import Contract
from EE.models import AcceptedContracts
from EE.models import ReleasedContracts
from EE.models import VolatileContracts

from EE.logic import decision as decision_logic
from EE.logic import game_state as game_state_logic
from EE.logic import game as game_logic

from EE.views import handler

class Home(handler.Handler):

	def authenticated(self):
		return self.profile_type == 'Groep'
		
	def get(self):
		context = self.getContext()
		game = Game.objects.filter(id=self.kwargs['id_str']).get()
		context['game'] = game
		context['acceptedContracts'] = AcceptedContracts.objects.filter(user=self.user).all()
		volatile = []
		new_arr = []
		players = game_logic.getPlayers(game)
		for u in players:
			volatile.append(VolatileContracts.objects.filter(decision=decision_logic.getDecision(game_state_logic.getState(u))).all())
		for v in volatile:
			for item in v:
				new_arr.append(item)
		context['releasedContracts'] = ReleasedContracts.objects.filter(game=game).all()
		if (len(new_arr) > 0):
		  context['volatileContracts'] = new_arr
		content = loader.render_to_string('news/home.html', dictionary=context)
		return http.HttpResponse(content)